package org.ksoap2.serialization;

import java.io.IOException;
import org.kobjects.base64.Base64;
import org.xmlpull.v1.XmlPullParser;
import org.xmlpull.v1.XmlPullParserException;
import org.xmlpull.v1.XmlSerializer;

public class MarshalBase64
  implements Marshal
{
  public static Class BYTE_ARRAY_CLASS = new byte[0].getClass();

  public Object readInstance(XmlPullParser parser, String namespace, String name, PropertyInfo expected)
    throws IOException, XmlPullParserException
  {
    return Base64.decode(parser.nextText());
  }

  public void writeInstance(XmlSerializer writer, Object obj) throws IOException {
    writer.text(Base64.encode((byte[])(byte[])obj));
  }

  public void register(SoapSerializationEnvelope cm) {
    cm.addMapping(cm.xsd, "base64Binary", BYTE_ARRAY_CLASS, this);
    cm.addMapping("http://schemas.xmlsoap.org/soap/encoding/", "base64", BYTE_ARRAY_CLASS, this);
  }
}
